


public class LockQueue implements Queue {

	static public Object lockObject = new Object();

	public class Node {
		public int value;
		public Node next;
		public Node (int val) {
			value = val;
			next = null;                        
		}		
	}
	
	private Node head = null;
	private Node tail = null;
	
	@Override
	public int deq(int tid) throws Exception {
		Node newNode = null;
		synchronized (lockObject) {
			if (head != null){
				newNode = head;
				head = head.next;
				if (null == head)
					tail = null;
			}
		}
		if (newNode == null) {
			throw new Exception();
		} 
		return newNode.value;
	}

	@Override
	public void enq(int tid, int value) {
		Node newNode = new Node(value);
		synchronized (lockObject) {
			if (null == head){
				head = tail = newNode;
			}
			else{
				tail.next = newNode;
				tail = newNode;
			}
		}
	}

}
